﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 三角形设计器
    /// </summary>
    public class TrianglesGeoDesigner : GeoDesignerBase
    {
        public TrianglesGeoDesigner()
        {
            handler3 = new HandlerThumb() { Shape = HandlerThumbShape.Rhombus };
            this.Handlers.Add(handler3);
            this.Direction = new Vector(100, 0);
            this.OtherPoint = new Point(0, 50);

            handler3.DragDelta += Handler3_DragDelta;
        }



        #region 控制点拖动处理
        private void Handler3_DragDelta(object sender, DragDeltaEventArgs e)
        {
            e.Handled = true;
            var v = new Vector(e.HorizontalChange, e.VerticalChange);
            this.OtherPoint += v;
        }


        #endregion


        private readonly HandlerThumb handler3;//另外一点的手柄 

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == OtherPointProperty)
            {
                rePositionHandlers();
            }
        }

        protected override void rePositionHandlers()
        {
            var op = this.OriginalPoint;
            var target = op + this.Direction;
            var p1 = this.OtherPoint;
            handler3.Center = p1;

            //生成Path用于渲染 
            this.pathGeometry.Clear();
            PathFigure pf = new PathFigure();
            this.pathGeometry.Figures.Add(pf);
            pf.StartPoint = op;
            pf.Segments.Add(new LineSegment(target, true));
            pf.Segments.Add(new LineSegment(p1, true));
            pf.IsClosed = true;

            base.rePositionHandlers();
        }


        /// <summary>
        /// 另一点的坐标
        /// </summary>
        public Point OtherPoint
        {
            get { return (Point)GetValue(OtherPointProperty); }
            set { SetValue(OtherPointProperty, value); }
        }

        public static readonly DependencyProperty OtherPointProperty =
            DependencyProperty.Register("OtherPoint", typeof(Point), typeof(TrianglesGeoDesigner), new PropertyMetadata(default(Point)));


    }
}
